package com.zhiyou100.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zhiyou100.mapper.UserMapper;
import com.zhiyou100.model.Result;
import com.zhiyou100.model.User;
import com.zhiyou100.service.UserService;

@Service
public class UserServiceImpl implements UserService{

	@Autowired
	private UserMapper userMapper;
	public UserMapper getUserMapper() {
		return userMapper;
	}
	public void setUserMapper(UserMapper userMapper) {
		this.userMapper = userMapper;
	}

	@Override
	public Result insertUser(User user) {
		int i = userMapper.insertUser(user);
		Result result = new Result(false, "新增user失败");
		if(i>0){
			result.setFlag(true);
			result.setMessage("新增user成功");
		}
		return result;
	}
	@Override
	public Result selectAll() {
		List<User> user=userMapper.selectAll();
		Result result=new Result(false,"查询user失败！");
		for (User user2 : user) {
			System.out.println(user2);
			result.setFlag(true);
			result.setMessage("查询user成功！");
		}
		return result;   
	}
	@Override
	public Result delete(int id) {
		int i=userMapper.deleteById(id);
		System.out.println(i);
		Result result=new Result(false,"删除失败！");
        if(i>0) {
        	result.setFlag(true);
        	result.setMessage("删除成功！");
        }

		return result;
	}
	@Override
	public Result update(User user) {
		User u=new User();
		u.setUsername("李四");
	    u.setId(2);
	    Result result=new Result(false,"修改失败");
       int i=userMapper.update(u);
	  if(i>0) {
		  result.setFlag(true);
		  result.setMessage("修改成功");
	  }
        return result;
	}
}
